package org.erp.spu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.erp.common.page.PageQueryModel;
import org.erp.spu.entity.SpuReleaseInfo;
import org.erp.spu.entity.bo.SpuReleaseInfoBo;
import org.erp.spu.entity.dto.SpuReleaseInfoDto;
import org.erp.spu.entity.vo.SpuReleaseInfoVo;
import org.erp.spu.mapper.SpuReleaseInfoMapper;
import org.erp.spu.service.SpuReleaseInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpuReleaseInfoServiceImpl extends ServiceImpl<SpuReleaseInfoMapper, SpuReleaseInfo> implements SpuReleaseInfoService {

    @Resource
    private SpuReleaseInfoMapper spuReleaseInfoMapper;

    @Override
    public List<SpuReleaseInfoVo> getList(SpuReleaseInfoBo spuReleaseInfoBo) {
        LambdaQueryWrapper<SpuReleaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(spuReleaseInfoBo.getName()), SpuReleaseInfo::getName, spuReleaseInfoBo.getName());
        queryWrapper.eq(spuReleaseInfoBo.getParentId() != null, SpuReleaseInfo::getParentId, spuReleaseInfoBo.getParentId());
        queryWrapper.eq(spuReleaseInfoBo.getType() != null, SpuReleaseInfo::getType, spuReleaseInfoBo.getType());
        queryWrapper.eq(StringUtils.hasText(spuReleaseInfoBo.getStatus()), SpuReleaseInfo::getStatus, spuReleaseInfoBo.getStatus());
        queryWrapper.eq(spuReleaseInfoBo.getReleaseGroupId() != null, SpuReleaseInfo::getReleaseGroupId, spuReleaseInfoBo.getReleaseGroupId());
        queryWrapper.eq(spuReleaseInfoBo.getCategoryId() != null, SpuReleaseInfo::getCategoryId, spuReleaseInfoBo.getCategoryId());
        queryWrapper.eq(SpuReleaseInfo::getDelFlag, 0);
        List<SpuReleaseInfo> list = spuReleaseInfoMapper.selectList(queryWrapper);
        return list.stream().map(this::convertToVo).collect(Collectors.toList());
    }

    @Override
    public PageQueryModel<SpuReleaseInfoVo> getListByPage(SpuReleaseInfoDto spuReleaseInfoDto) {
        LambdaQueryWrapper<SpuReleaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(spuReleaseInfoDto.getName()), SpuReleaseInfo::getName, spuReleaseInfoDto.getName());
        queryWrapper.eq(spuReleaseInfoDto.getParentId() != null, SpuReleaseInfo::getParentId, spuReleaseInfoDto.getParentId());
        queryWrapper.eq(spuReleaseInfoDto.getType() != null, SpuReleaseInfo::getType, spuReleaseInfoDto.getType());
        queryWrapper.eq(spuReleaseInfoDto.getReleaseGroupId() != null, SpuReleaseInfo::getReleaseGroupId, spuReleaseInfoDto.getReleaseGroupId());
        queryWrapper.eq(spuReleaseInfoDto.getCategoryId() != null, SpuReleaseInfo::getCategoryId, spuReleaseInfoDto.getCategoryId());
        queryWrapper.eq(StringUtils.hasText(spuReleaseInfoDto.getStatus()), SpuReleaseInfo::getStatus, spuReleaseInfoDto.getStatus());
        queryWrapper.eq(SpuReleaseInfo::getDelFlag, 0);
        Page<SpuReleaseInfo> page = this.page(spuReleaseInfoDto.buildPage(), queryWrapper);
        List<SpuReleaseInfoVo> voList = page.getRecords().stream().map(this::convertToVo).collect(Collectors.toList());
        return PageQueryModel.of(page.getPages(), voList);
    }

    @Override
    public boolean addSpuReleaseInfo(SpuReleaseInfoDto spuReleaseInfoDto) {
        Assert.isTrue(!checkNameExists(spuReleaseInfoDto), "发布项名称已存在");
        SpuReleaseInfo entity = new SpuReleaseInfo();
        BeanUtils.copyProperties(spuReleaseInfoDto, entity);
        return spuReleaseInfoMapper.insert(entity) > 0;
    }



    @Override
    public boolean updateSpuReleaseInfo(SpuReleaseInfoDto spuReleaseInfoDto) {
        Assert.isTrue(spuReleaseInfoDto.getId() != null, "发布项ID不能为空");
        SpuReleaseInfo existing = spuReleaseInfoMapper.selectById(spuReleaseInfoDto.getId());
        Assert.isTrue(existing != null, "发布项不存在");
        Assert.isTrue(!checkNameExists(spuReleaseInfoDto), "发布项名称已存在");
        BeanUtils.copyProperties(spuReleaseInfoDto, existing);
        return spuReleaseInfoMapper.updateById(existing) > 0;
    }

    @Override
    public SpuReleaseInfoVo getSpuReleaseInfoVoById(Long id) {
        SpuReleaseInfo entity = spuReleaseInfoMapper.selectById(id);
        return convertToVo(entity);
    }

    /**
     * 检查发布项名称是否存在
     *
     * @param dto
     * @return
     */
    private boolean checkNameExists(SpuReleaseInfoDto dto) {
        if (!StringUtils.hasText(dto.getName())) {
            return false;
        }
        LambdaQueryWrapper<SpuReleaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuReleaseInfo::getName, dto.getName());
        queryWrapper.eq(SpuReleaseInfo::getType, dto.getType());
        queryWrapper.eq(SpuReleaseInfo::getReleaseGroupId, dto.getReleaseGroupId());
        queryWrapper.eq(SpuReleaseInfo::getDelFlag, 0);
        if (dto.getId() != null) {
            queryWrapper.ne(SpuReleaseInfo::getId, dto.getId());
        }
        return spuReleaseInfoMapper.selectCount(queryWrapper) > 0;
    }

    private SpuReleaseInfoVo convertToVo(SpuReleaseInfo entity) {
        if (entity == null) {
            return null;
        }
        SpuReleaseInfoVo vo = new SpuReleaseInfoVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }
}
